#ifndef VCD_COMMON_H
#define VCD_COMMON_H
#include "config.h"
#include <memory.h>
#include <sys/time.h>
#include <time.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifndef __WIN32__
#include <net/if.h>
#include <sys/ioctl.h>
#endif
#ifdef __linux__
#include <linux/if.h>
#endif
#include "openssl/aes.h"
#include "openssl/bio.h"
#include "openssl/evp.h"
#include "openssl/pem.h"

#ifdef __WIN32__
#include <windows.h>
#endif

#if defined(WIN32) || defined(WIN64)
#define snprintf _snprintf
#endif

#ifdef __linux__
#define VCD_PIC_PATH "/usr/share/spice-gtk/img/"
#elif __APPLE__
#define VCD_PIC_PATH "/usr/local/var/spice-dir/img/"
//#define VCD_PIC_PATH "./Contents/Resources/share/img/"
#else
#define VCD_PIC_PATH "../share/img/"
#endif

#define PVE_API_TOKEN_KEY  "wsagent@pve!IDV"
#define PVE_API_TOKEN_CONF "/etc/pve/priv/token.cfg"

#include "vcd-util.h"
#include "vcd-log.h"

G_BEGIN_DECLS

typedef struct net_window_ctx{
    guint timer;

    int image_compression;
    int video_codec_type;
    int screen_width;
    int screen_height;

    int frame_rate;
    float video_rate;

    float network_delay;
    float network_pkt_loss_rate;
}StNetWindowCtx ;

void set_img(GtkWidget *win, const char *widget_class, const char *pic_name);
void set_css(GtkWidget *widget, const gchar *data_css);
gboolean is_alnum_dot(char *str);
gboolean is_num_dot(char *str);
gboolean is_num_dot_colon(char *str);
gboolean is_digit(const char *str);
gboolean ends_with(char const *str, char const *suffix);
gboolean has_char(char const *str, char c);
int getlen(char *result);
int aes_decrypt(char* str_in, char* out);
int aes_encrypt_extend(char* str_in,int len, char* out);
int aes_decrypt_extend(char* str_in, int len,char* out);
int aes_encrypt(char* str_in, char* out);
int base64_encode(char *in_str, int in_len, char *out_str);
int base64_decode(char *in_str, int in_len, char *out_str);
char *strlower(char *str);
char *strupper(char *str);
char *ltrim(char *str);
/*
 * exe system command
 * return: result
*/
char* exe_command(const char* cmd, size_t* len);

//return  microseconds
uint64_t get_time(void);

//sort vm by name
void sort_vm_info(vm_info *f, gint count);

void vm_config_response_free(vm_config_response* resp);

void vcd_common_set_app_icon(GtkWidget* win,const char *name);

GtkWidget* vcd_net_window_new(GtkWidget *overlay);
void vcd_net_window_set_screen_size(int width,int height);

gboolean check_is_process_exist(char *process_name,gboolean is_kill_it);

void vcd_common_url_decode(char* url);

void vcd_common_set_img_path(const char* path);
char* vcd_common_get_absolute_img_path(const char* name);

char* vcd_common_resolve_domain(const char* domain);
void vcd_common_init_platform();
void vcd_common_init_some_path(const char* arg0,spice_connect_data* scd);
void update_spice_info_sync(spice_connect_data *scd);

char* vcd_common_get_unix_sock(int vmid);
char* vcd_common_get_apitoken();

G_END_DECLS
#endif
